<!--

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */

function createObject()
{
	var request_type;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		request_type = new XMLHttpRequest();
	}
	
	return request_type;
}

var http = createObject();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

var nocache = 0;

function login()
{
	document.getElementById('login_response').innerHTML = '<img src="../images/ajax-loader.gif" /><p class="login">Authenticating ...</p>';
	setTimeout("dologin()",2000);
}

function loggedIn(sess_id)
{
	window.location = 'home.php?id='+sess_id;
}

function dologin()
{
	// Optional: Show a waiting message in the layer with ID ajax_response
	//document.getElementById('login_response').innerHTML = "Loading..."
	//setTimeout("loginStartMsg()",3000);
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var usr = encodeURI(document.getElementById('usrn').value);
	var psw = encodeURI(document.getElementById('upwd').value);
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'login_auth.php?uname='+usr+'&pswd='+psw+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}

function loginReply()
{
	if(http.readyState == 4)
	{
		var response = http.responseText; 
		response = response.replace(/^\s+/, ''); //added replace func to remove leading spaces that comes in sometimes.
		alert(response);
		if(response == 'O')
		{
			// if login fails
			document.getElementById('login_response').innerHTML = '<p class="login_fail"><img src="../images/exclamation.png" /> Login failed! Verify username & password</p>';
			// else if login is ok show a message: "Welcome + the user name".
		}
		else
		{
			document.getElementById('login_response').innerHTML = '<p class="login_success">Logged in. Redirecting ...</p>';
			//document.getElementById('login_response').innerHTML = 'Welcome '+response;
			setTimeout(function(){loggedIn(response)},1000);
		}
	}
}

-->